<template>
    <div class="login" style="overflow: auto;">
        <!--动态登录-->
        <div class="header" @click="$router.push({name:'pwdLogin'})">密码登录</div>
        <div class="lhs">龙虎山</div>
<div class="info1">
    <div class="tel">
        <input type="text" placeholder="手机号" v-model="uphone">
    </div>
    <div class="getauto" @click="getcode">{{counter}}</div>
</div>
    <div class="auto">
        <input type="text" placeholder="动态码"  v-model="code">
    </div>

        <div class="btnLogin" @click="dologin">登录</div>
        <div class="xieyi"></div>
    </div>
</template>

<script>
    import { Toast } from 'mint-ui';
    export default {
        name: "autoLogin",
        data(){
            return{
                hid:'',
                uphone:'',
                code:'',
                ising:false,
                counter:'获取动态码',

            }
        },
        methods:{
            getcode:async function(){
                let res =  await  this.$api.post('/user/reg',{
                    telphone:this.uphone,
                    code:this.code,
                    type:2

                });
                console.log(res);
                if(res.data.state){
                    if(this.ising){
                        Toast({
                            message: '正在获取验证码',
                        });
                        return false;
                    }

                    this.ising=true;
                 //倒计时
                    this.counter=60;
                    this.timer=setInterval(()=>{
                        this.counter --;
                        if(this.counter==0){
                            this.ising=false;
                            this.counter="重新获取";
                            clearInterval(this.timer);
                        }
                    },1000);
                    Toast({
                        message: res.data.info._code,

                    });

                }else{
                    Toast({
                        message: '失败'+res.data.info,
                        iconClass: 'icon icon-cuowushibaibukeyong'

                    });
                }

            },

            dologin:async function(){
                let res =  await  this.$api.post('/user/login',{
                    telphone:this.uphone,
                    code:this.code,
                    type:2
                });

                console.log(res);

                if(res.data.state){
                    Toast({
                        message: "正在登陆,请稍等...",
                        iconClass: 'icon icon-success'
                    });

                    this.$store.commit('updateUserinfo',res.data.data.userinfo);
                    setTimeout(()=>{
                        if (this.hid){
                            this.$router.replace({ name: 'detail',query:{hid:this.hid} }) ;
                        } else {
                            this.$router.replace({ name: 'usercenter' })
                        }

                    },2000) ;
                }else{
                    Toast({
                        message: '失败'+res.data.info,
                        iconClass: 'icon icon-cuowushibaibukeyong'

                    });
                }

            },
        },
        created:async function(){
            this.hid=this.$route.query.hid;
            console.log(this.hid);
             /*if(this.$store.state.userinfo.uid){
                 Toast({
                     message: '您已登录,请不要重复登录',

                 });
                 return false;
             }else{

             }*/
        }
    }
</script>

<style scoped>

</style>